//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package bulletin_dao

import (
	"context"

	"atomgit.com/openlinksaas/api-server/dao/db_ns"
	"atomgit.com/openlinksaas/proto-gen-go.git/project_bulletin_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type BulletinInfoDbItem struct {
	BulletinId   string `bson:"_id"`
	ProjectId    string `bson:"projectId"`
	Title        string `bson:"title"`
	CreateUserId string `bson:"createUserId"`
	CreateTime   int64  `bson:"createTime"`
	UpdateUserId string `bson:"updateUserId"`
	UpdateTime   int64  `bson:"updateTime"`
}

func (item *BulletinInfoDbItem) ToBulletinInfoKey() *project_bulletin_api.BulletinInfoKey {
	return &project_bulletin_api.BulletinInfoKey{
		BulletinId:   item.BulletinId,
		Title:        item.Title,
		CreateUserId: item.CreateUserId,
		CreateTime:   item.CreateTime,
		UpdateUserId: item.UpdateUserId,
		UpdateTime:   item.UpdateTime,
	}
}

type _BulletinInfoDao struct {
	mc *mongo.Collection
}

func newBulletinInfoDao(db *mongo.Database) (*_BulletinInfoDao, error) {
	mc := db.Collection(db_ns.DB_NS_PROJECT_BULLETIN + "info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"projectId", 1}, {"createTime", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_BulletinInfoDao{mc: mc}, nil
}

func (db *_BulletinInfoDao) Insert(ctx context.Context, dbItem *BulletinInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_BulletinInfoDao) UpdateTitle(ctx context.Context, bulletinId, projectId, title, updateUserId string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": bulletinId, "projectId": projectId},
		bson.M{"$set": bson.M{
			"title":        title,
			"updateUserId": updateUserId,
			"updateTime":   updateTime,
		}})
	return err
}

func (db *_BulletinInfoDao) Remove(ctx context.Context, bulletinId, projectId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": bulletinId, "projectId": projectId})
	return err
}

func (db *_BulletinInfoDao) Exist(ctx context.Context, bulletinId, projectId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": bulletinId, "projectId": projectId})
	return count > 0, err
}

func (db *_BulletinInfoDao) Get(ctx context.Context, bulletinId, projectId string) (*BulletinInfoDbItem, error) {
	retItem := &BulletinInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": bulletinId, "projectId": projectId}).Decode(retItem)
	return retItem, err
}

func (db *_BulletinInfoDao) Count(ctx context.Context, projectId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"projectId": projectId})
	return uint32(count), err
}

func (db *_BulletinInfoDao) List(ctx context.Context, projectId string, offset, limit int64) ([]*BulletinInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"projectId": projectId},
		options.Find().SetSkip(offset).SetLimit(limit).SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*BulletinInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_BulletinInfoDao) ListById(ctx context.Context, projectId string, bulletinIdList []string) ([]*BulletinInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"_id": bson.M{"$in": bulletinIdList}, "projectId": projectId})
	if err != nil {
		return nil, err
	}
	retList := []*BulletinInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_BulletinInfoDao) DistinctBulletIdByProject(ctx context.Context, projectId string) ([]string, error) {
	itemList, err := db.mc.Distinct(ctx, "_id", bson.M{"projectId": projectId})
	if err != nil {
		return nil, err
	}
	retList := []string{}
	for _, item := range itemList {
		retList = append(retList, item.(string))
	}
	return retList, nil
}
